<?php

namespace app\common\model;

use think\Model;

class Order extends BaseModel
{
    public function add($data)
    {
        if (!is_array($data)) {
            exception('数据错误');
        }
        $data['status'] = 1;
        $this->allowField(true)->save($data);
        return $this->id;
    }

    //支付成功，数据更新
    public function updateOrderByOutTradeNo($out_trade_no, $weixinData)
    {
        $data = [];
        if (!empty($weixinData['transaction_id'])) {
            $data['transaction_id'] = $weixinData['transaction_id'];
        }
        if (!empty($weixinData['total_fee'])) {
            $data['total_fee'] = $weixinData['total_fee'] / 100;
            $data['pay_status'] = 1;
            $data['pay_time'] = date('Y-m-d H:i:s', strtotime($weixinData['time_end']));
        }
        return $this->allowField(true)->save($data, ['out_trade_no' => $out_trade_no]);
    }
}
